import axios from 'axios'
import { ElNotification } from 'element-plus'

export const instance = axios.create({
  baseURL: import.meta.env.VITE_API_URL,
  timeout: import.meta.env.VITE_TIMEOUT,
})

instance.interceptors.response.use(
  ({ data }) => {
    if (data.code !== 200) {
      ElNotification({
        title: '请求失败',
        message: data?.message,
        type: 'error',
        position: 'top-left',
        duration: 0,
      })
      return Promise.reject(data)
    } else {
      return data
    }
  },
  ({ data }) => {
    ElNotification({
      title: '请求失败',
      message: '服务器连接超时',
      type: 'error',
      position: 'top-left',
      duration: 0,
    })
    return Promise.reject(data)
  },
)
